/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Apr 5, 2009, 2:23:58 PM
 */

package ui;

import controllers.UIController;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * <b>MainFrame</b>
 * 
 * <p>This is the main JFrame for the application.</p>
 */
public class MainFrame extends javax.swing.JFrame
{

    /** Creates new form MainFrame */
    public MainFrame()
    {
        setLookAndFeel();
        setScreenStartPoint();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitPane = new javax.swing.JSplitPane();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        directoryItem = new javax.swing.JMenuItem();
        exitItem = new javax.swing.JMenuItem();
        viewMenu = new javax.swing.JMenu();
        listItem = new javax.swing.JRadioButtonMenuItem();
        visualItem = new javax.swing.JRadioButtonMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Have a little Java with your search!");
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(500, 500));
        setName("mainFrame"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                altFFourPressed(evt);
            }
        });

        splitPane.setBackground(new java.awt.Color(255, 255, 255));
        splitPane.setDividerLocation(240);
        splitPane.setDividerSize(7);
        splitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        splitPane.setResizeWeight(0.7);
        splitPane.setContinuousLayout(true);
        splitPane.setLastDividerLocation(240);
        splitPane.setOneTouchExpandable(true);

        fileMenu.setText("File");
        fileMenu.setPreferredSize(new java.awt.Dimension(29, 25));

        directoryItem.setText("Search directory");
        directoryItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                directoryItemActionPerformed(evt);
            }
        });
        fileMenu.add(directoryItem);

        exitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        exitItem.setText("Exit");
        exitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitItem);

        menuBar.add(fileMenu);

        viewMenu.setText("View");

        listItem.setSelected(true);
        listItem.setText("List");
        listItem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                listItemStateChanged(evt);
            }
        });
        viewMenu.add(listItem);

        visualItem.setSelected(false);
        visualItem.setText("Visual");
        visualItem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                visualItemStateChanged(evt);
            }
        });
        viewMenu.add(visualItem);

        menuBar.add(viewMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Event handler for exiting the program from the menu bar
    private void exitItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitItemActionPerformed
    {//GEN-HEADEREND:event_exitItemActionPerformed
        exitApplication();
    }//GEN-LAST:event_exitItemActionPerformed

    // Exit program from Alt-F4 keypress
    private void altFFourPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_altFFourPressed
    {//GEN-HEADEREND:event_altFFourPressed
       if(evt.isAltDown() && evt.getKeyCode() == KeyEvent.VK_F4)
       {
           exitApplication();
       }
    }//GEN-LAST:event_altFFourPressed

    // Changes the current view to a traditional list view for search results
    private void listItemStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_listItemStateChanged
    {//GEN-HEADEREND:event_listItemStateChanged
        if (listItem.isSelected())
        {
            visualItem.setSelected(false);
            UIController.getInstance().setListConfiguration();
        }
        else if (!listItem.isSelected() && !visualItem.isSelected())
        {
            listItem.setSelected(true);
        }
    }//GEN-LAST:event_listItemStateChanged

    // Changes the current view to an information visualization view for search results
    private void visualItemStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_visualItemStateChanged
    {//GEN-HEADEREND:event_visualItemStateChanged
        if (visualItem.isSelected())
        {
            listItem.setSelected(false);
            UIController.getInstance().setVisualConfiguration();
        }
        else if (!visualItem.isSelected() && !listItem.isSelected())
        {
            visualItem.setSelected(true);
        }
    }//GEN-LAST:event_visualItemStateChanged

    // Resets the position of the divider after a frame-resize
    private void formComponentResized(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentResized
    {//GEN-HEADEREND:event_formComponentResized
       UIController.getInstance().defaultSplitterPosition();
    }//GEN-LAST:event_formComponentResized

    // Resets the search directory
    private void directoryItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_directoryItemActionPerformed
    {//GEN-HEADEREND:event_directoryItemActionPerformed
        UIController.getInstance().getMainDirectory();
    }//GEN-LAST:event_directoryItemActionPerformed

    /**
     * Displays the frame to the user
     */
    public void Show()
    {
        this.setVisible(true);
    }

    /**
     * Sets the query panel for the application
     * @param c - instance of <code>QueryPanel</code> to display to user
     */
    public void setTopPanel(QueryPanel c)
    {
        if (c != null)
        {
            splitPane.setTopComponent(c);
        }
    }

    /**
     * Sets the panel for displaying
     * @param c - <code>JComponent</code> for listing results from a query
     */
    public void setBottomPanel(JComponent c)
    {
        if (c != null)
        {
            //JScrollPane pane = new JScrollPane(c);
            resultsPane = null;
            resultsPane = new JScrollPane(c);
            splitPane.setBottomComponent(resultsPane);
        }
    }

    /**
     * Sets the position of the divider for the <code>JSplitPane</code>
     * @param percent - <code>double</code> value that represents a percentage of the screen.
     * The value for <code>percent</code> must be between 0 and 1.
     */
    public void setSplitterPosition(double percent)
    {
        splitPane.setDividerLocation(percent);
    }

    /**
     * Sets the panels for executing queries and displaying results
     * @param qp - JPanel for entering queries
     * @param rd - JPanel for displaying search results as either a list or visual format
     */
    public void setConfiguration(QueryPanel qp,
                                 JPanel rd)
    {
        setTopPanel(qp);
        setBottomPanel(rd);      
    }

    // Offset the screen from the top left corner
    private void setScreenStartPoint()
    {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        this.setLocation((int)(0.2 * screenSize.width),
                         (int)(0.2 * screenSize.height));
    }

    // Sets the program's look-and-feel to that of the machine
    private void setLookAndFeel()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } 
        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (InstantiationException ex)
        {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (UnsupportedLookAndFeelException ex)
        {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Operations for exiting the program cleanly.
    private void exitApplication()
    {
        setVisible(false);
        this.dispose();
        System.exit(0);
    }

    private JScrollPane resultsPane;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem directoryItem;
    private javax.swing.JMenuItem exitItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JRadioButtonMenuItem listItem;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JMenu viewMenu;
    private javax.swing.JRadioButtonMenuItem visualItem;
    // End of variables declaration//GEN-END:variables

}
